<template>
  <div>
    <div class="panel panel-default">你的账户需要激活后使用</div>
    <el-form
        ref="activateFormRef"
        :model="activateFormValue"
        status-icon
        :rules="rules"
        label-width="200"
        class="form-inline"
    >
      <el-form-item label="账户" prop="account">
        <el-input v-model="activateFormValue.account" :prefix-icon="User" placeholder="请输入账号"/>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model="activateFormValue.password" type="password" :prefix-icon="Lock" placeholder="请输入密码"
                  autocomplete="off"/>
      </el-form-item>
      <el-form-item label="确认密码" prop="repeatPassword">
        <el-input v-model="activateFormValue.repeatPassword" type="password" :prefix-icon="Lock" placeholder="请输入密码"
                  autocomplete="off"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm(activateFormRef)">
          Submit
        </el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script lang="ts" setup>
import { reactive, ref } from 'vue'
import type { FormInstance, FormRules } from 'element-plus'
import {Lock, User} from '@element-plus/icons-vue';
const activateFormRef = ref<FormInstance>()
import SystemUser from '@/api/SystemUser.js';
import md5 from 'js-md5';
import {useRouter} from "vue-router";

const router = useRouter()


const validateAccount = (rule: any, value: any, callback: any) => {
  if (value === '') {
    callback(new Error('请输入账户'))
  }
  callback()
}
const validatePassword = (rule: any, value: any, callback: any) => {
  if (value === '') {
    callback(new Error('请输入密码'))
  } else {
    if (activateFormValue.repeatPassword !== '') {
      if (!activateFormRef.value) return
      activateFormRef.value.validateField('repeatPassword')
    }
    callback()
  }
}
const validateRepeatPassword = (rule: any, value: any, callback: any) => {
  if (value === '') {
    callback(new Error('请输入密码'))
  } else if (value !== activateFormValue.password) {
    callback(new Error("两次输入密码不一致"))
  } else {
    callback()
  }
}

const activateFormValue = reactive({
  account: '',
  password: '',
  repeatPassword: '',
})

const rules = reactive<FormRules<typeof activateFormValue>>({
  account: [{ validator: validateAccount, trigger: 'blur' }],
  password: [{ validator: validatePassword, trigger: 'blur' }],
  repeatPassword: [{ validator: validateRepeatPassword, trigger: 'blur' }],
})

const submitForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.validate((valid) => {
    if (valid) {
      const body = ref({
        account:activateFormValue.account,
        password: md5(activateFormValue.password),
        repeatPassword: md5(activateFormValue.repeatPassword),
      })
      SystemUser.activate(body.value)
          .then(res => {
            router.push({ name: 'home'});
          })
    }
  })
}
</script>

<style scoped>
.form-inline {
  margin: 20px;
}
.form-inline .el-input {
  --el-input-width: 200px;
}
</style>